﻿//百度地图
var map = new BMap.Map("bad_box");
map.centerAndZoom("重庆", 12);
map.enableScrollWheelZoom(true);
var opts = {
    width: 250,     // 信息窗口宽度
    height: 100,     // 信息窗口高度
    title: "餐厅信息", // 信息窗口标题
    enableMessage: true//设置允许信息窗发送短息
};
// 编写自定义函数,创建标注
function addMarker(point, name, driver, address) {

    var content = "店铺名：" + name + "<br/>司机：" + driver + "<br/>地址：" + address;

    var marker = new BMap.Marker(point);
    map.addOverlay(marker);
    var label = new BMap.Label(name, { offset: new BMap.Size(20, -10) });
    label.setStyle({
        color: "#fff",
        fontSize: "12px",
        height: "20px",
        lineHeight: "8px",
        fontFamily: "微软雅黑",
        backgroundColor: "#ff0000",
        maxWidth: "none",
        borderRadius: "5px",
        padding: "5px",
        opacity: "0.7"
    });
    marker.setLabel(label);
    marker.addEventListener("click", function (e) {
        openInfo(content, e)
    }
    );
}

function openInfo(content, e) {
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象
    map.openInfoWindow(infoWindow, point); //开启信息窗口
}

function LoadMap(factoryid, keyword) {
    var url = "";
    if (factoryid != null) {
        url = "/Restaurant/GetRestaurantDistributedData?id=" + factoryid + "&keyword=" + keyword;
    }
    else {
        url = "/Restaurant/GetRestaurantDistributedData?keyword=" + keyword;
    }

    $.getJSON(url).done(function (data) {
        CommonAjaxCallback(data, function () {
            map.clearOverlays();
            if (data.result.length > 0) {
                for (var i = 0; i < data.result.length; i++) {
                    var driver = "";
                    if (data.result[i].employeeDriver != null) {
                        driver = data.result[i].employeeDriver.name;
                    }
                    var point = new BMap.Point(data.result[i].longitude, data.result[i].latitude);
                    addMarker(point, data.result[i].restaurantName, driver, data.result[i].restaurantAddress);
                }
            }
        })
    });

}
$(document).ready(function () {

    $.getJSON("/Factory/GetAll").done(function (data) {
        CommonAjaxCallback(data, function () {
            if (data.result.length > 0) {
                var d = $("#external_events");
                for (var i = 0; i < data.result.length; i++) {
                    var t;
                    if (i == 0) {
                        t = "<div class='external-event navy-bg active' id='" + data.result[i].id + "'>" + data.result[i].factoryName + "</div>";

                    } else {
                        t = "<div class='external-event navy-bg' id='" + data.result[i].id + "'>" + data.result[i].factoryName + "</div>";
                    }
                    d.append(t);
                }
                LoadMap(data.result[0].id, "");
            }
        })
    })

    //setInterval('LoadMap(null,"")', 300000);

    $('#external_events').on('click', '.navy-bg', function () {
        map.clearOverlays();
        $(this).addClass('active').siblings('.navy-bg').removeClass('active');
        var id = $(this).attr("id");
        LoadMap(id, "");
    })

    $('#search').click(function () {
        var id = $('#external_events').children('.active').attr("id");

        var keyword = $('#keyword').val();
        LoadMap(id, keyword);
    });
})
